package main

import (
	"fmt"
	"time"

	"gopkg.in/go-module/log.v1"

	"gitee.com/Flearning/go_crawl/modules"
	"gitee.com/Flearning/go_crawl/modules/mgobson"
	"gitee.com/Flearning/go_crawl/modules/xueqiu/search"
	"gitee.com/Flearning/go_crawl/setting"
	"gitee.com/Flearning/go_crawl/utils"
)

const (
	DAY_LAYOUT = "2006-01-02"
	LAYOUT     = "2006-01-02 15:04:05"
)

func main() {
	setting.NewConfigContext()
	setting.NewLogService()

	mgobson.NewInit(setting.InfoMgo)
	mgobson.MgoSession = mgobson.CopyMgoConn()

	mr := new(modules.ReqCrawlInfo)
	rb, err := mr.ReadCrawlInfoFile()
	if err != nil {
		log.Fatal(4, "Read file Error: %v", err)
	}
	search.ReqCfg = mr.New(rb)
	search.MgoXueqiuCollection = search.ReqCfg.CrawlCollection
	search.MgoStripCollection = search.ReqCfg.StripCollection

	crawlRate := setting.RateCrawl

	loc, _ := time.LoadLocation("Asia/Shanghai")

	for {
		now := time.Now()
		toDay := now.Format(DAY_LAYOUT)
		start := fmt.Sprintf("%s %s", toDay, crawlRate.StartTime)
		end := fmt.Sprintf("%s %s", toDay, crawlRate.EndTime)
		theUnixTime := now.Unix()
		timeStart, _ := time.ParseInLocation(LAYOUT, start, loc)
		timeEnd, _ := time.ParseInLocation(LAYOUT, end, loc)

		toWeekday := now.Weekday()
		inWeeks := utils.InWeeks(int(toWeekday), crawlRate.Weeks)
		// fmt.Println(theUnixTime, timeStart.Unix(), timeEnd.Unix())
		log.Info("the time: %s -- startTime: %s --- endTime: %s", now.String(), timeStart.String(), timeEnd.String())
		log.Info("the day weekday: %d, is inWeeks %v.", toWeekday, inWeeks)
		if inWeeks {
			if theUnixTime < timeEnd.Unix() && theUnixTime > timeStart.Unix() {
				RunCrawl()
			}
		}
		time.Sleep(time.Duration(crawlRate.Rate) * time.Minute)
	}
}

func RunCrawl() {
	mySched := search.NewScheduler()
	var (
		symbol, sotr     string
		page, statusCode int
		sleep            int64
		respBodyBtye     []byte
	)

	for _, val := range search.ReqCfg.Prames {
		symbol = val.Symbol
		page = val.Page
		sotr = val.Sort
		sleep = val.Sleep
		source = val.Source
		mySched = mySched.Init(symbol, page, sotr)
		for {
			statusCode, respBodyBtye = mySched.Start()
			bsonD := mySched.Run(statusCode, respBodyBtye)
			log.Info("Currently Crawl Data Result：%v", bsonD)
			sign := mySched.HandleData(bsonD...)
			fmt.Println("sign:", sign)
			mySched.GetName("uriPath")
			if statusCode != 200 {
				break
			}
			if sign {
				break
			}
			time.Sleep(time.Duration(sleep) * time.Millisecond)
		}
		time.Sleep(time.Duration(sleep) * time.Millisecond)
	}

}
